<?php
/** 
 * Cruise_model
 * This class gathers cruise info from the database
 * 
 * @author Ray Nowell
 *	
 */ 
class Cruise_model extends CI_Model {

	function Cruise_model()
	{
		parent::__construct();
	}
	
	/*
	 * Returns a list of upcoming cruises
	 */
	function Load_cruise_info($cruise_id = false)
	{
		
		$select_fields =	'DATE_FORMAT(cruise_date,\'%c/%e/%Y\') as cruise_date,
							 DATEDIFF(cruise_date,CURDATE()) as days_till_cruise,
							 name,
							 place,
							 note';
		$this->db->select($select_fields, false);
		$this->db->from('cruise');
                if (!$cruise_id)
                    $this->db->where('cruise_date >=', date('Y/m/d'));                 
                else
                    $this->db->where('cruise_id =', $cruise_id); 
                
		$this->db->order_by('days_till_cruise'); 
                    
		$query = $this->db->get();
		 
		return $query->result_array();
	}

        function Load_past_cruise_info()
	{
		
		$select_fields =    'cruise.cruise_id,
                                        DATE_FORMAT(cruise_date,\'%c/%e/%Y\'),
					name,
					place,
                                        COUNT(cg.cg_id) as image_count';
		$this->db->select($select_fields, false);
		$this->db->from('cruise');
                $this->db->join('cruise_gallery AS cg','cruise.cruise_id = cg.cruise_id','left');
		$this->db->where('cruise_date <', date('Y/m/d')); 
                $this->db->group_by(array('cruise_date', 'name','place')); 
		$this->db->order_by('cruise_date', 'desc'); 
		$query = $this->db->get();
		 
		return $query->result_array();
	}

	/*
	 * Gets infor required to load past cruises
	 */
	function Load_cruise_gallery()
	{
		$this->db->select('c.cruise_id, 
							CONCAT(name,\' (\',place,\') -- \',DATE_FORMAT(cruise_date,\'%c/%e/%Y\')) AS cruise_info,
							pic_name',false);
		$this->db->from('cruise c');
		$this->db->join('cruise_gallery cg', 'c.cruise_id = cg.cruise_id');
		$this->db->order_by('cruise_date', 'desc'); 
		$this->db->order_by('pic_name', 'asc'); 
		return $this->db->get();
	}
    
    function Get_cruise_selection_list()
	{
		$this->db->select("c.cruise_id, 
                           CONCAT_WS(' -- ',`name`, Place, DATE_FORMAT(cruise_date,'%c/%e/%Y')) AS cruise",false);
		$this->db->from('cruise c');
		$this->db->order_by('name', 'asc'); 
		$query = $this->db->get();
        
        return $query->result_array();
	}
        
    function Get_cruise_gallery($cruise_id) {
	$select_fields =    "pic_name";
		$this->db->select($select_fields, false);
		$this->db->from('cruise_gallery');
		$this->db->where('cruise_id', $cruise_id);
        $query = $this->db->get();
		 
	return $query->result_array();
    }
    
    function Add_cruise($data)
	{
		$this->db->insert('cruise', $data);
		return $this->db->insert_id();
	}
    
    function Add_cruise_gallery_image($data)
	{
		$this->db->insert('cruise_gallery', $data);
		return $this->db->insert_id();
	}

}

/* End of file cruise_model.php */
/* Location: ./system/application/models/cruise_model.php */